package com.mdd.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mdd.product.entity.LeasingScheme;
import com.mdd.product.mapper.LeasingSchemeMapper;
import com.mdd.product.service.ILeasingSchemeDetailsService;
import com.mdd.product.service.ILeasingSchemeService;
import com.mdd.product.validate.LeasingSchemeCreateValidate;
import com.mdd.product.validate.LeasingSchemeUpdateValidate;
import com.mdd.product.vo.LeasingSchemeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * 租赁方案实现类
 *
 * @author LikeAdmin
 */
@Service
public class LeasingSchemeServiceImpl implements ILeasingSchemeService {

    @Resource
    LeasingSchemeMapper leasingSchemeMapper;
    @Resource
    ILeasingSchemeDetailsService iLeasingSchemeDetailsService;

    /**
     * 租赁方案列表
     *
     * @param spuId 商品id
     * @return List<LeasingSchemeListedVo>
     * @author LikeAdmin
     */
    @Override
    public List<LeasingSchemeVo> list(Long spuId) {
        LambdaQueryWrapper<LeasingScheme> leasingWrapper = new LambdaQueryWrapper<>();
        leasingWrapper.eq(LeasingScheme::getSpuId, spuId);
        List<LeasingScheme> leasingSchemes = leasingSchemeMapper.selectList(leasingWrapper);

        List<LeasingSchemeVo> list = new LinkedList<>();
        for (LeasingScheme item : leasingSchemes) {
            LeasingSchemeVo vo = new LeasingSchemeVo();
            BeanUtils.copyProperties(item, vo);
            vo.setSchemeDetails(iLeasingSchemeDetailsService.list(spuId));
            list.add(vo);
        }
        return list;
    }


    /**
     * 租赁方案详情
     *
     * @param id 主键参数
     * @return OmsLeasingScheme
     * @author LikeAdmin
     */
    @Override
    public LeasingSchemeVo detail(Long id) {
        LeasingScheme model = leasingSchemeMapper.selectById(id);

        Assert.notNull(model, "租赁方案详细数据不存在");

        LeasingSchemeVo vo = new LeasingSchemeVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 租赁方案新增
     * @param createValidate 参数
     * @author LikeAdmin
     */

    @Override
    @Transactional
    public Long add(LeasingSchemeCreateValidate createValidate) {
        LeasingScheme model = new LeasingScheme();
        model.setSpuId(createValidate.getSpuId());
        model.setLeaseType(createValidate.getLeaseType());
        model.setMonthlyRent(createValidate.getMonthlyRent());
        model.setWeeklyRent(createValidate.getWeeklyRent());
        model.setDayRant(createValidate.getDayRant());
        leasingSchemeMapper.insert(model);
        return model.getId();
    }

    /**
     * 租赁方案编辑
     *
     * @param updateValidate 参数
     * @author LikeAdmin
     */
    @Transactional
    @Override
    public void edit(LeasingSchemeUpdateValidate updateValidate) {
        LeasingScheme model = leasingSchemeMapper.selectById(updateValidate.getId());

        Assert.notNull(model, "租赁方案数据不存在!");

        model.setSpuId(updateValidate.getSpuId());
        model.setLeaseType(updateValidate.getLeaseType());
        model.setMonthlyRent(updateValidate.getMonthlyRent());
        model.setWeeklyRent(updateValidate.getWeeklyRent());
        model.setDayRant(updateValidate.getDayRant());
        leasingSchemeMapper.updateById(model);
    }

    /**
     * 租赁方案删除
     *
     * @param id 主键ID
     * @author LikeAdmin
     */
    @Transactional
    @Override
    public void del(Long id) {
        LeasingScheme model = leasingSchemeMapper.selectById(id);

        Assert.notNull(model, "租赁方案数据不存在!");

        leasingSchemeMapper.deleteById(id);
    }

    @Override
    public void delIds(List<Object> id) {
        leasingSchemeMapper.deleteBatchIds(id);
    }

    @Override
    public List<Object> listDeletedIds(Long spuId, Set<Long> idSet) {
        LambdaQueryWrapper<LeasingScheme> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(LeasingScheme::getId);
        if (!idSet.isEmpty()) {
            queryWrapper.notIn(LeasingScheme::getId, idSet);
        }
        queryWrapper.eq(LeasingScheme::getId, spuId);
        return leasingSchemeMapper.selectObjs(queryWrapper);
    }

}
